<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<script>
    var socket;
    //判断当前浏览器是否支持websocket
    if(window.WebSocket) {
        //go on
        socket = new WebSocket("ws://192.168.0.93:8888/hello");
        //相当于channelReado, ev 收到服务器端回送的消息
        socket.onmessage = function (ev) {
        var data=ev.data.split(",");
        var divChild = document.getElementById("box");
        divChild.style.left=data[0]+"px";
        divChild.style.top=data[1]+"px";
        divChild.style.position="absolute";
            rt.value = rt.value + "\n" + ev.data;
        }

        //相当于连接开启(感知到连接开启)
        socket.onopen = function (ev) {
            alert("连接开启了..")
            var rt = document.getElementById("responseText");
            rt.value = "连接开启了.."
        }

        //相当于连接关闭(感知到连接关闭)
        socket.onclose = function (ev) {

            var rt = document.getElementById("responseText");
            rt.value = rt.value + "\n" + "连接关闭了.."
        }
    } else {
        alert("当前浏览器不支持websocket")
    }

    //发送消息到服务器
    function send(message) {
        if(!window.socket) { //先判断socket是否创建好
            return;
        }
        if(socket.readyState == WebSocket.OPEN) {
            //通过socket 发送消息
            socket.send(message)
        } else {
            alert("连接没有开启");
        }
    }
    function move(){
        var e = window.event;
        send(e.clientX+","+e.clientY)
    }

</script>
<body style="height: 1080px;width: 1920px" onmousemove="move()" touchmove="move()">

<form onsubmit="return false">
    <div id="box" style="background: #0c5460;height: 80px;width: 80px;" onclick="changecolor()"></div>
</form>
</body>
</html>